package com.wj.controller;

import com.wj.pojo.Admin;
import com.wj.pojo.AdminLoginParam;
import com.wj.pojo.RespBean;
import com.wj.service.IAdminService;
import com.wj.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录控制器
 * @author wj
 * @since 1.0.0
 */
@Api(tags = "登录控制器")
@RestController
public class LoginController {
    @Autowired
    IAdminService adminService;

    @ApiOperation("登录之后返回Token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam loginParam, HttpServletRequest request){
        return adminService.login(loginParam.getUsername(), loginParam.getPassword(),loginParam.getCaptcha(),request);
    }

    @ApiOperation("注销账号")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功");
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(@ApiIgnore Principal principal){
        if (principal == null)return null;
        Admin admin = adminService.getAdminByUserName(principal.getName());
        admin.setPassword(null);
        admin.setRoles(adminService.getRoles(admin.getId()));
        return admin;
    }
}
